package com.bjpowernode.service.impl;

import com.bjpowernode.dao.ProvideDao;
import com.bjpowernode.dao.UserDao;
import com.bjpowernode.service.ProviderService;
import com.bjpowernode.user.Provide;
import com.bjpowernode.user.User;
import com.bjpowernode.util.OSSUtil;
import com.bjpowernode.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ProviderServiceImpl implements ProviderService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ProvideDao provideDao;

    @Override
    public int addFood(String username, MultipartFile file, String foodName, Float foodPrice, Integer foodCount) throws IOException {
        User selectUser = userDao.selectUser(username);
        String fileName = file.getOriginalFilename();
        String fileType = "";
        if (fileName.contains(".")){
            fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        String path = Util.getTempDir() + File.separator + System.currentTimeMillis() + "." + fileType;
        file.transferTo(new File(path));
        String fileUrl = OSSUtil.fileUpload(path);
        Provide provide = new Provide(null, selectUser.getId(), foodName,fileUrl,foodPrice,foodCount,new Date(),null);
        return provideDao.insert(provide);
    }

    @Override
    public int addFood(String username, InputStream in, String foodName, Float foodPrice, Integer foodCount) throws IOException {
        String path = Util.getTempDir() + File.separator + System.currentTimeMillis() + ".jpg";
        File tempFile = new File(path);
        while (!tempFile.exists())
            tempFile.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        byte[] bytes = new byte[1024];
        int count = 0;
        while ((count = in.read(bytes)) != -1) {
            outputStream.write(bytes,0,count);
        }
        outputStream.flush();
        outputStream.close();
        String fileUrl = OSSUtil.fileUpload(path);
        User selectUser = userDao.selectUser(username);
        Provide provide = new Provide(null, selectUser.getId(), foodName,fileUrl,foodPrice,foodCount,new Date(),null);
        return provideDao.insert(provide);
    }

    @Override
    public int delFood(Integer id) {
        return provideDao.removeById(id);
    }

    @Override
    public List<Provide> selectUserFood(String username) {
        User selectUser = userDao.selectUser(username);
        return provideDao.selectFoodByUserId(selectUser.getId());
    }

    @Override
    public List<Provide> findProviderOrder(Integer id) {
        return provideDao.selectFoodByUserId(id);
    }

    @Override
    public List<Provide> loadAllFood() {
        return provideDao.loadAllFood();
    }

    @Override
    public Provide loadProvideFood(Integer foodId) {
        return provideDao.loadProvideFood(foodId);
    }

    @Override
    public int updateCount(Integer foodId) {
        return provideDao.updateCount(foodId);
    }
}
